package com.hmall.cart.Listener;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmall.cart.domain.po.Cart;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.CartClearMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @Author：cxl
 * @Package：com.hmall.trade.listense
 * @Project：hmall-micro1
 * @name：PaySuccessListtener
 * @Date：2025/11/14 11:21
 * @Filename：PaySuccessListtener 下单成功后异步清理购物车
 */
@Component
@Slf4j
public class TradeSuccessListener {
    @Autowired
    private ICartService cartService;

    //TRADE_EXCHANGE_NAME, MqConstants.TRADE_SUCCESS_KEY,
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstants.TRADE_SUCCESS_QUEUE),
            exchange = @Exchange(MqConstants.TRADE_EXCHANGE_NAME),
            key = MqConstants.TRADE_SUCCESS_KEY
    ))
    public void listen(CartClearMessageDTO dto) {
        log.info("监听到TRADE成功，清理购物车");
        cartService.remove(Wrappers.<Cart>lambdaQuery()
                .eq(Cart::getUserId, dto.getUserId())
                .in(Cart::getItemId, dto.getItemIds()));
    }

}
